package com.drought.rained.view;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.drought.rained.R;

public class NormalDialogLJ extends Dialog implements View.OnClickListener {

    private View rootView;
    private TextView tv_tile;
    private TextView tv_content;
    private Button left_button;
    private Button right_button;
    private NormalClickListener mNormalClickListener;

    public NormalDialogLJ(@NonNull Context context) {
        super(context);
        init(context);
    }

    public NormalDialogLJ(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        init(context);
    }

    protected NormalDialogLJ(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        init(context);
    }

    private void init(Context context) {
        rootView = LayoutInflater.from(context).inflate(R.layout.layout_normal_dialog, null);
        tv_tile = rootView.findViewById(R.id.tv_tile);
        tv_content = rootView.findViewById(R.id.tv_content);
        left_button = rootView.findViewById(R.id.left_button);
        right_button = rootView.findViewById(R.id.right_button);
        setContentView(rootView);

        Window window = getWindow();
        assert window != null;
        WindowManager.LayoutParams params = window.getAttributes();
        params.gravity = Gravity.CENTER;
        // 设置对话框大小
        params.width = context.getResources().getDimensionPixelOffset(R.dimen.dialog_width);
        window.setAttributes(params);

        left_button.setOnClickListener(this);
        right_button.setOnClickListener(this);
        setCanceledOnTouchOutside(false);
    }


    public NormalDialogLJ setTitle(String title) {
        if (tv_tile != null) {
            tv_tile.setText(title);
        }
        return this;
    }


    public NormalDialogLJ setContent(String contentStr) {
        if (tv_content != null) {
            tv_content.setText(contentStr);
        }
        return this;
    }


    public NormalDialogLJ setLeftBtnText(String leftText) {
        if (left_button != null) {
            left_button.setText(leftText);
        }
        return this;
    }


    public NormalDialogLJ setRightBtnText(String rightText) {
        if (right_button != null) {
            right_button.setText(rightText);
        }
        return this;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.left_button:
                if (mNormalClickListener != null) {
                    mNormalClickListener.onLeftClick();
                }
                break;
            case R.id.right_button:
                if (mNormalClickListener != null) {
                    mNormalClickListener.onRightClick();
                }
                break;
        }
    }


    public NormalDialogLJ setNormalClickListener(NormalClickListener listener) {
        mNormalClickListener = listener;
        return this;
    }

    public interface NormalClickListener {
        void onLeftClick();

        void onRightClick();
    }
}
